//
//  WangqiVC.m
//  SSC
//
//  Created by 红沙尘 on 17/6/8.
//  Copyright © 2017年 红沙尘. All rights reserved.
//

#import "WangqiVC.h"
#import "WangqiCell.h"
#import "RMDateSelectionViewController.h"

@interface WangqiVC ()<RMDateSelectionViewControllerDelegate>
{
    __weak IBOutlet UIButton *searchBtn;
    __weak IBOutlet UIView *dateView;
    __weak IBOutlet UITextField *dateField;
    
    NSMutableArray *dataArray;
    
    NSInteger page;
}

@property (nonatomic, assign) CaipiaoType otype;

@end

@implementation WangqiVC

- (instancetype)initWithType:(CaipiaoType)type {
    self = [super init];
    
    if (self) {
        self.otype = type;
    }
    
    return self;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    if (dataArray.count == 0) {
        page = 1;
        [mainTableView.mj_footer beginRefreshing];
    } else {
        [self refresh];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setTitle:@"往期开奖"];
    self.navigationItem.leftBarButtonItem = CUSTOMVIEW([UIView new]);
}

- (IBAction)search {
    [mainTableView.mj_footer beginRefreshing];
}

- (IBAction)selectDate {
    
    RMDateSelectionViewController *dateVC = [RMDateSelectionViewController dateSelectionController];
    dateVC.delegate = self;
    dateVC.disableBouncingWhenShowing = NO;
    dateVC.disableMotionEffects       = YES;
    dateVC.disableBlurEffects         = YES;
    dateVC.blurEffectStyle = UIBlurEffectStyleExtraLight;
    [dateVC.datePicker setTimeZone:[NSTimeZone timeZoneWithName:@"GMT+8"]];
    dateVC.datePicker.maximumDate = [NSDate date];
    [dateVC.datePicker setDatePickerMode:UIDatePickerModeDate];
    [dateVC show];
}

- (void)refresh {
    page = 1;
    [self loadMoreData];
}

- (void)loadData {
    
    [self loadMoreData];
}

- (void)loadMoreData {
    __weak UITableView *weakTableView = mainTableView;
    
    [SSCRequest lotteryHistoryListAtDate:[MTool formartDate:dateField.text]
                                    page:page
                                    type:self.otype
                                 success:^(NSDictionary *responseObject)
     {
         [weakTableView.mj_header endRefreshing];
         [weakTableView.mj_footer endRefreshing];
         
         if (page == 1) [dataArray removeAllObjects];
         
         NSArray *data = responseObject[@"data"];
         
         [dataArray addObjectsFromArray:data];
         [mainTableView reloadData];
         
         if (data.count >= SSC_PAGE_SIZE) {
             page ++;
         }
         
     } fail:^(NSError *error) {
         [weakTableView.mj_header endRefreshing];
         [weakTableView.mj_footer endRefreshing];
     }];
}

#pragma mark - RMDateSelectionViewControllerDelegate

- (void)dateSelectionViewController:(RMDateSelectionViewController *)vc didSelectDate:(NSDate *)aDate {

    dateField.text = [MTool dateToString:aDate];
    [mainTableView.mj_header beginRefreshing];
}

#pragma mark - tableview

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WangqiCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WangqiCell"];
    
    [cell loadWithDict:dataArray[indexPath.row]];
    
    return cell;
}

#pragma mark -

- (void)setupUI {
    
    dateField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
    dateField.leftViewMode = UITextFieldViewModeAlways;
    
    searchBtn.layer.cornerRadius  = 2;
    searchBtn.layer.masksToBounds = YES;
    searchBtn.layer.borderColor = COLOR_FF5253.CGColor;
    searchBtn.layer.borderWidth = LineHeight;
    
    dateView.layer.cornerRadius  = 2;
    dateView.layer.masksToBounds = YES;
    dateView.layer.borderColor = COLOR_CCCCCC.CGColor;
    dateView.layer.borderWidth = LineHeight;

    mainTableView.rowHeight = 83;
    mainTableView.estimatedRowHeight = 83;
    [mainTableView registerNib:[UINib nibWithNibName:@"WangqiCell" bundle:nil]
        forCellReuseIdentifier:@"WangqiCell"];
}

- (void)setupData {
    
    dataArray = [[NSMutableArray alloc] init];

    dateField.text = [MTool dateToString:[NSDate date]];
    
    mainTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self
                                                                   refreshingAction:@selector(loadData)];
    
    mainTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self
                                                               refreshingAction:@selector(refresh)];
}

@end
